<template>
  <div class="loginWrapper">
    <div class="contentWrapper">
      <h1 class="f-zhong" @click="do_goHome"><img src="../assets/login/minzi2019626.png" alt=""> <img src="../assets/login/lianjie2019626.png" alt=""></h1>
      <div class="content">
        <div class="contentLeft">

        </div>
        <div class="contentRight">
          <h5 @click="test">注册</h5>
          <div class="inputGroup" v-for="(item,index) in inputList" :key="index">
           <span class="img"><img :src="item.imgSrc" alt="" :class="item.class"></span>
            <input :type="item.type" :placeholder="item.placeholder" v-model="inputList[index].model" :maxlength="item.maxLength">
            <button v-if="index==2" class="codeButton text-center" @click="do_getCode" v-text="btntxt" :disabled="disabled" :style="{background:getCode==true?'grey':'#41bafe' }"></button>
          </div>
          <button class="reset" @click="def_reset"> 重置</button>
          <div class="inputGroup"> <button class="regist" @click="do_regist"> 注册 </button></div>
        </div>

      </div>
    </div>
  </div>
</template>

<script>

  export default {
    name: "BS_cgkLogin",
    data(){
      return{
        zcDomain:this.$ZcPublic.Domain,
        inputList:[
          {imgSrc:this.$ZcPublic.Domain+'static/RSImg/xingming.png',maxLength:'',type:"text",placeholder:'请输入姓名',model:'',class:''},
          // {imgSrc:this.$ZcPublic.Domain+'static/RSImg/danwei.png',maxLength:'',type:"text",placeholder:'请输入单位',model:'',class:'unit'},
          {imgSrc:this.$ZcPublic.Domain+'static/RSImg/phone (2).png',maxLength:'11',type:"text",placeholder:'请输入手机号',model:'',class:'tel'},
          {imgSrc:this.$ZcPublic.Domain+'static/RSImg/yanzhengma.png',maxLength:'',type:"text",placeholder:'请输入验证码',model:'',class:''},
          {imgSrc:this.$ZcPublic.Domain+'static/RSImg/mima.png',maxLength:'',type:"text",placeholder:'初始密码默认“0000” ',model:'',class:''},
        ],
        btntxt:'获取验证码',
        getCode:false,
        disabled:false
      }
    },
    methods:{
      /*错误框函数*/
      def_mistake(){
        // alert(99)
        this.mistake=false
      },
      /*提醒选择框确认*/
      def_makeSure(){
        this.confirm=false
      },
      /*
      * 提醒选择框取消*/
      def_giveUp(){
        this.confirm=false
      },
      test(){

      },
      def_time() {
        this.getCode=true
        if (this.time > 0) {
          this.time--;
          this.btntxt=this.time+"s后重新获取";
          setTimeout(this.def_time, 1000);
        } else{
          this.time=0;
          this.btntxt="获取验证码";
          this.disabled=false;
          this.getCode=false
        }
      },
      do_help(){

      },
      do_getCode(){
        try {

          let me=this;
          let reg=11 && /^[1]([0-9])[0-9]{9}$/;
          if(me.inputList[1].model===''){
            this.$confirm({
              type:'错误',
              msg:'输入手机号！',
              btn:{
                ok:'yes'
              }
            })
          }else if(!reg.test(me.inputList[1].model)){
            this.$confirm({
              type:'错误',
              msg:'手机号格式错误！',
              btn:{
                ok:'yes'
              }
            })
          } else{
            me.time=60;
            me.disabled=true;
            me.def_time();
            me.post_getCode();

          }
        }catch (e) {
          alert(e)
          // zcLogger.Log({
          //   Vue : 'zcPerson.vue',
          //   Fun : 'doAdd',
          //   Msg : e.message
          // });
        }
      },
      def_reset() {
        this.inputList.forEach((item) => {
          item.model = ''
        })
        this.time = 0;
        this.btntxt = "获取验证码";
      },
      post_getCode(){//获取验证码
        let me=this
        me.$ZcPublic.zcServer.Post({
          IsNeedLogin: 'false',
          Paras: {
            Name: 'Vue_resultLib',
            action: "Ext_PhoneCode",
            Data: {
              OrganCode:me.$ZcPublic.OrganCode,
              Telephone:me.inputList[1].model
            }
          },
          Callback:  me.cb_getCode,
          Scope: me,
        });

      },
      cb_getCode(resJson,scoped){
        console.log("验证码",resJson)
        if(!resJson.success){
          this.$confirm({
            type:'错误',
            msg:resJson.msg,
            btn:{
              ok:'yes'
            }
          })
        }
      },
      do_regist(){
        let me = this;
        me.$ZcPublic.zcServer.Post({
          IsNeedLogin: 'false',
          Paras: {
            Name: 'Vue_resultLib',
            action: "Ext_ProjectLeaderRegister",
            data: {
               "Telephone": me.inputList[1].model,
               "UserName": me.inputList[1].model,
               "Name": me.inputList[0].model,
               "Password": me.inputList[3].model||'0000',
               "VerifyCode": me.inputList[2].model
            }
          },
          Callback:  me.cb_do_regist,
          Scope: me,
        });
      },
      cb_do_regist(resJson,scoped){
        console.log("注册返回",resJson)
        if(resJson.success){
          this.$toast({
            icon:'ok',
            msg:'注册成功'
          })
          if (localStorage.UserCode) {  //如果保存有自动登录的移除信息
            localStorage.removeItem('UserCode')
            localStorage.removeItem('Password')
          }
          this.do_Login()
        }else {
          this.$confirm({
            type:'错误',
            msg:resJson.msg,
            btn:{
              ok:"yes"
            }
          })
        }
      },
      do_Login() {
        let me = this
        me.$ZcPublic.zcServer.Post({
          IsNeedLogin: 'false',
          Paras: {
            Name: 'Vue_LoginResultLib',
            action: "Login",
            Data: {
              Password: me.inputList[3].model||'0000',
              UserCode: me.inputList[1].model,
              OrganCode: me.$ZcPublic.OrganCode
              // GB_OrganSN: me.$ZcPublic.GB_OrganSN
            }
          },
          Callback: me.cb_login2,
          Scope: me,
        })

      },
      cb_login2(resJson, scoped) {
        try {
          console.log('登录返回', resJson)
          if (resJson.success) {
            this.$ZcPublic.needLogin=true
            let _info = resJson.data
            let _userName = _info.Name.split('(')[0]
            let _telephone =  _info.Name.split('(')[1].split(')')[0]
            localStorage.setItem('userName', _userName)
            localStorage.setItem('telephone', _telephone)
            sessionStorage.setItem('isLogin', true)

            let me =this
            setTimeout(()=>{
              if(localStorage.getItem("JumpKey") != 'null' && localStorage.getItem("JumpKey") != '' && localStorage.getItem("JumpKey") != undefined) {
                window.location.href = localStorage.getItem("JumpKey")
                localStorage.removeItem('JumpKey')
              }else {
                me.$router.push('/BS_cgkHome')
              }

            },1600)

          } else {
            this.$confirm({
              type: '错误',
              msg: resJson.msg,
              btn: {
                ok: "yes"
              }
            })
          }
        }catch (e) {
          console.log(e)
        }

      },
      do_goHome(){
        this.$router.push('/BS_cgkHome')
      }
    }
  }
</script>

<style scoped>
  @import "../common/css/login-regist.css";
  *{
    margin: 0;
    padding: 0;
    text-align: left
  }
  .inputGroup .img .unit{
    width: 22px;
  }
  .inputGroup .img .tel{
    width: 18px;
  }
</style>
